package droidninja.filepicker.load;

import android.app.Dialog;

/**
 * <p>Title: LoadingDialog </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/3/6 17:10
 */
public class LoadingDialog {
    private Dialog mProgressDialog;

    public LoadingDialog(Dialog dialog) {
        this.mProgressDialog = dialog;
    }


    /**
     * 显示加载进度
     */
    public void showLoading() {
        showLoading("正在加载...");
    }

    /**
     * 显示加载进度
     */
    public void showLoading(String title) {
        showLoading(title, true);
    }

    /**
     * 设置点击弹窗外面是否关闭弹窗，默认为true
     *
     * @param title 要显示的文字
     */
    public void showLoading(String title, boolean isDismissOutside) {
        showLoading(title, isDismissOutside, isDismissOutside);
    }

    public void showLoading(int titleId) {
        if (mProgressDialog == null) {
            return;
        }
        showLoading(mProgressDialog.getContext().getResources().getString(titleId));
    }

    public void showLoading(String title, boolean isDismissOnBackPressed, boolean isDismissOnTouchOutside) {
        if (mProgressDialog == null) {
            return;
        }
        mProgressDialog.setTitle(title);
        mProgressDialog.setCanceledOnTouchOutside(isDismissOnTouchOutside);
        mProgressDialog.setCancelable(isDismissOnBackPressed);
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    public void hideLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    public void onDestroy() {
        hideLoading();
        mProgressDialog = null;
    }
}

